package com.cqust.icc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 张成
 * @version 1.0
 * @date 2022/4/7 15:14
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Transactional(propagation = Propagation.REQUIRED,
            isolation = Isolation.DEFAULT, readOnly = false,rollbackFor = Exception.class)
    public void Transfer(Account out,Account in,String str) {
        in = getById(in.getAccountName());

        float outnum = Float.parseFloat(getById(out.getAccountName()).getAccountBalance());
        float num = Float.parseFloat(str);

        in.setAccountBalance(String.valueOf(Float.parseFloat(in.getAccountBalance()) +num));
        updateAccount(in);
        if (outnum<num){
            throw new RuntimeException("余额不足");
        }

        Update(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));

    }

    public void addAccount(Account account) {
        System.out.println("addAccount");
        System.out.println(account);
        //创建sql语句
        String sql=" INSERT INTO zcc.account (account_name, account_balance) VALUES (?, ?) ";
        // 调用方法实行
        int updat = jdbcTemplate.update(sql,account.getAccountName(),account.getAccountBalance());
        // 返回成功的条数
//        System.out.println(updat);

    }

    public List<Account> selectAllAccountList() {
        String sql = "select * from zcc.account";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Account>(Account.class));

    }

    public void updateAccount(Account account) {
        System.out.println("updateAccount");
        System.out.println(account);
        String sql = "UPDATE   zcc.account SET     account_balance = ? WHERE account_name = ?";
        Object[] args = {account.getAccountBalance(),account.getAccountName()};
        int updat = jdbcTemplate.update(sql,args);
//        System.out.println(updat);

    }

    public void deleteAccountById(int id) {
        String sql ="DELETE FROM  zcc.account WHERE id = ?";
        int updat = jdbcTemplate.update(sql,id);
//        System.out.println(updat);


    }

    public void withdrawMoney(Account account) {
        System.out.println("withdrawMoney");
        System.out.println(account);
        String sql = "UPDATE   zcc.account SET   account_balance = account_balance-? WHERE account_name = ?";
        Object[] args = {account.getAccountBalance(),account.getAccountName()};
        int updat = jdbcTemplate.update(sql,args);
    }

    private void query() {
        List<LogEntity> query = jdbcTemplate.query("select * from tb_log", new BeanPropertyRowMapper<LogEntity>(LogEntity.class));
        System.out.println(query);
    }

    public void Update(String str) {
        String sql = "UPDATE tb_log set method_name = 'update by  " + str + "' WHERE zcc.tb_log.id =7";
        int update = jdbcTemplate.update(sql);
        if (update > 0) {
            System.out.println("更新成功");
        } else {
            System.out.println("更新失败");
        }
    }

    public Account getById(String str) {
        String sql = "select * from  zcc.account   WHERE account.account_name = '"+str+"'";
        List<Account> logEntityList = jdbcTemplate.query(sql, new Object[]{}, new BeanPropertyRowMapper<Account>(Account.class));
        if (logEntityList.size() > 0) {
            return logEntityList.get(0);

        }
        return
                null;
    }

}
